//
//  NewCarViewController.m
//  LoveCar
//
//  Created by Xue Senhong on 15/5/26.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "NewCarViewController.h"

@interface NewCarViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)NSMutableArray *bigArray;

@property (nonatomic, retain)UITableView *tableView;
@end

@implementation NewCarViewController

- (void)dealloc
{
    [_bigArray release];
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.bigArray = [NSMutableArray array];
    }
    return self;
}

- (void)connectUrl
{
    [SANetWorkingTask request:@"http://api.ycapp.yiche.com/car/getserialinfoforNew" parmater:nil block:^(NSDictionary *result) {
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            NewCarModel *model = [[[NewCarModel alloc] init] autorelease];
            [model setValuesForKeysWithDictionary:dic];
            [self.bigArray addObject:model];
            [model release];
        }
        [self.tableView reloadData];
    }];
}

- (void)loadView
{
    [super loadView];
    self.navigationController.navigationBar.translucent = NO;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0,kScreenHeight * 20 / 667)];
    titleLabel.textColor = [UIColor blueColor];
    titleLabel.text = @"上市新车";
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    [self connectUrl];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[NewCarCell class] forCellReuseIdentifier:@"new"];
    [self.view addSubview:self.tableView];
    [self.tableView release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kScreenHeight * 100 / 667;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bigArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewCarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"new"];
    NewCarModel *model = [self.bigArray objectAtIndex:indexPath.row];
    [cell.picView sd_setImageWithURL:[NSURL URLWithString:model.Img] placeholderImage:nil];
    cell.nameLabel.text = model.ShowName;
    cell.priceLabel.text = model.Price;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailCarViewController *detail = [[DetailCarViewController alloc] init];
    NewCarModel *model = [self.bigArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
    detail.cellModel.serialId = [NSString stringWithFormat:@"%ld", (long)model.CSId];
    [detail release];
}
@end
